package com.lbc.common.config.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author xyl
 * @date 2021-03-03 9:29
 */
@Configuration
public class ShiroConfig {


    @Bean("securityManager")
    public SecurityManager securityManager(UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        securityManager.setRememberMeManager(null);
        return securityManager;
    }


    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        //添加自定义的认证授权过滤器
        Map<String, Filter> filters = new HashMap<>(1);
        filters.put("auth", new AuthFilter());
        shiroFilter.setFilters(filters);

        //添加拦截路径
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/scroller/list", "anon");
        filterMap.put("/donate-project/front/*", "anon");
        filterMap.put("/donate/notify", "anon");
        filterMap.put("/donate/list", "anon");
        filterMap.put("/donate/statistics", "anon");
        filterMap.put("/picture/list", "anon");
        filterMap.put("/news/list/random", "anon");
        filterMap.put("/admin/login", "anon");
        filterMap.put("list", "anon");
        filterMap.put("/kid/front/**", "anon");
        filterMap.put("/messageBoard/list", "anon");

        filterMap.put("/admin/**","auth");
        filterMap.put("/assist/**","auth");
        filterMap.put("/kid/**","auth");
        filterMap.put("/news/add", "auth");
        filterMap.put("/news/remove/**", "auth");
        filterMap.put("/news/modify", "auth");
        filterMap.put("/news/like/**", "auth");
        filterMap.put("/picture/**", "auth");
        filterMap.put("/user/**", "auth");
        filterMap.put("/scroller/**", "auth");
        filterMap.put("/donate-project/**", "auth");
        filterMap.put("/donate/**", "auth");
        filterMap.put("/messageBoard/**", "auth");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        return shiroFilter;
    }
    /**
     * 配置以下bean借助SpringAOP扫描使用Shiro注解的类
     *
     * */
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn({"lifecycleBeanPostProcessor"})
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
